layui.config({
    base: '/src/'
}).use('index', function() {
    var $ = layui.$,
        admin = layui.admin,
        main = layui.main,
        setter = layui.setter,
        layer = layui.layer,
        dropdown = layui.dropdown,
        form = layui.form,
        table = layui.table;

    var pid = 0;

    //加载列表
    table.render({
        elem: '#LAY-app-area-list',
        url: setter.serverAdd + 'system/area/Query',
        where: {
            parentid: pid,
        },
        title: '数据表',
        toolbar: '#toolbarDemo',
        page: true,
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                { field: 'areanumber', title: '区域代码' },
                { field: 'areaname', title: '区域名称' },
                { field: 'shortareaname', title: '区域简称' }, { field: 'paixu', title: '排序' },
                { title: '下一级', width: 100, align: 'center', fixed: 'right', toolbar: '#tableoption' },
                { title: '操作', width: 140, align: 'center', fixed: 'right', toolbar: '#LAY-app-area-toolbar' }

            ]
        ],
        done: function(res) {
            dropdown.render({
                elem: '.rowMode',
                data: [
                    { id: 'default-row', title: '单行模式（默认）' }, { id: 'multi-row', title: '多行模式' }
                ],
                // 菜单被点击的事件
                click: function(obj) {
                    switch (obj.id) {
                        case 'default-row':
                            table.reload('LAY-app-area-list', { lineStyle: null }); //恢复单行
                            break;
                        case 'multi-row':
                            table.reload('LAY-app-area-list', { lineStyle: 'height: 95px;' });
                            break;
                    }
                }
            });
        }
    });

    //查询
    form.on('submit(table-search)', function(data) {
        var field = data.field;
        data.field.parentid = pid;
        table.reload('LAY-app-area-list', { page: { curr: 1 }, where: field });
        return false; //阻止默认form 跳转
    });

    form.on('submit(table-add)', function(data) {
        layer.open({
            type: 2,
            title: '添加企业信息',
            shadeClose: true,
            maxmin: true, //开启最大化最小化按钮
            area: ['60%', '80%'],
            content: setter.views + 'system/area/yj_system_area_add.html'
        });
    });


    //单元格工具事件 (编辑&更多)
    table.on('tool(LAY-app-area-list)', function(obj) {
        var data = obj.data; //获得当前行数据
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: '编辑',
                shadeClose: true,
                maxmin: true,
                area: ['60%', '80%'],
                content: setter.views + 'system/area/yj_system_area_edit.html?id=' + data.id
            });
        } else if (obj.event === 'more') { //下拉菜单-更多
            dropdown.render({
                elem: this, //触发事件的 DOM 对象
                show: true, //外部事件触发即显示
                data: [
                    { title: '详情', id: 'see' }, { title: '删除', id: 'del' }
                ],
                click: function(menudata) {
                    if (menudata.id === 'see') {
                        layer.open({
                            type: 2,
                            title: '详情',
                            shadeClose: true,
                            maxmin: true,
                            area: ['60%', '80%'],
                            content: setter.views + 'system/area/yj_system_area_info.html?id=' + data.id
                        });
                    } else if (menudata.id === 'del') {
                        layer.confirm('确认删除 ?', function(index) {
                            admin.req({
                                url: 'system/area/Delete',
                                type: 'delete',
                                data: { id: data.id },
                                done: function(res) {
                                    if (res.successful) {
                                        obj.del(); //删除对应行（tr）的DOM结构
                                        layer.close(index);
                                    }
                                    admin.msg_success(res.msg);
                                }
                            });
                        });
                    }
                },
                align: 'right', //右对齐弹出
                style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
            });
        } else if (obj.event == 'next') {
            pid = data.id;
            $("#parentid").val(pid);
            table.reload('LAY-app-area-list', {
                where: { // 传递数据异步请求时携带的字段
                    parentid: pid,
                },
            });
            if (pid != "0") {
                admin.req({
                    url: 'system/area/GetAreaPath',
                    type: 'get',
                    data: { id: pid },
                    done: function(res) {
                        $("#divpath").html(res.data);
                        $('.layuiadmin-btn-area-list').on('click', function() {
                            var type = $(this).data('type');
                            active[type] ? active[type].call(this) : '';
                        });
                    }
                });
            }
        }
    });
    $('.layuiadmin-btn-area-list').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //事件
    var active = {
        next: function() {
            var pid = $(this)[0].id;
            table.reload('LAY-app-area-list', {
                where: { // 传递数据异步请求时携带的字段
                    parentid: pid,
                },
            });
            admin.req({
                url: 'system/area/GetAreaPath',
                type: 'get',
                data: {
                    id: pid
                },
                done: function(res) {
                    $("#divpath").html(res.data);
                    $('.layuiadmin-btn-area-list').on('click', function() {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });
                }
            });
        }
    }
});